#!/bin/emacs --script

(require 'package)
(setq package-user-dir (expand-file-name "./.packages"))
(setq package-archives '(("melpa" . "https://melpa.org/packages/")
                         ("elpa" . "https://elpa.gnu.org/packages/")))

;; Initialize the package system
(package-initialize)
(unless package-archive-contents
  (package-refresh-contents))

;; Install dependencies
(package-install 'htmlize)

(require 'htmlize)

(require 'ox-publish)

(defun craggy-hurd-generate-a-href (filename local-dir label)
  (concat "<a href=\"" local-dir filename "\">" label "</a>"))

(defun craggy-hurd-generate-navbar (local-dir)
  "This will generate a navbar for you, but you need to specify a
list of filename links, and a local-dir, which can be './' or ../"
  (concat "<nav>"
	  (craggy-hurd-generate-a-href "index.html"	local-dir "Home")
	  (craggy-hurd-generate-a-href "about.html"	local-dir "About")
	  (craggy-hurd-generate-a-href "blog/index.html" local-dir "Blog")
	  "</nav>"))

(setq craggy-hurd-stylesheets
      '("normalize.css" "default-org-stylesheet.css" "stylesheet.css"))

(defun craggy-hurd-generate-stylesheets (filenames local-dir)
  "This generates an HTML link to a css file, based on the filename
and local-dir, which can be either './' or '../'"
  (interactive)
  (if (null filenames)
      ""
    (concat "<link rel=\"stylesheet\" href=\""
	    local-dir (car filenames)
	    "\" type=\"text/css\"/>"
	    (craggy-hurd-generate-stylesheets (cdr filenames) local-dir))))

(setq org-publish-project-alist
      `(("craggy-hurd"
	 :base-directory "."
	 :publishing-directory "./rendered/"
	 :auto-sitemap t
	 :section-numbers nil
	 :with-toc nil
	 :htmlized-source t
	 :html-head-include-default-style nil
	 :html-head ,(craggy-hurd-generate-stylesheets
		      craggy-hurd-stylesheets "./css/")
	 :html-preamble ,(craggy-hurd-generate-navbar "./"))
	("craggy-hurd-css"
	 :base-directory "./css/"
	 :publishing-function org-publish-attachment
	 :publishing-directory "./rendered/css/"
	 :base-extension "css")
	("craggy-hurd-blog"
	 :base-directory "./blog/"
	 :publishing-directory "./rendered/blog/"
	 :exclude "index.org"
	 :html-head ,(craggy-hurd-generate-stylesheets
		      craggy-hurd-stylesheets "../css/")
	 :html-head-include-default-style nil
	 :htmlized-source t
	 :section-numbers nil
	 :with-toc nil
	 :html-preamble ,(craggy-hurd-generate-navbar "../")
	 :auto-sitemap t
	 :sitemap-filename "index.org"
	 :sitemap-title "Blog"
	 :sitemap-sort-files anti-chronologically)
	))

(org-publish-all t)

(print "Build complete")
